package com.simple.util;

import com.simple.vo.GeneralName;
import com.sun.org.apache.xml.internal.security.utils.Base64;

import java.util.HashMap;
import java.util.Map;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

public class OCRAFanTiUtil {
	
	public static String ocr(String img) {
		try {
			Map<String, String> paramMap = new HashMap<String, String>();
			String randomStr = GeneralName.getUniqueName();
			String key = "yxp_20190808_qdsj";
			paramMap.put("id", randomStr);
			paramMap.put("co_id", "co_youxuepai");
			paramMap.put("submit_key", Base64.encode(RC4Util.encry_RC4_byte(randomStr, key)));
			Map<String, byte[]> fileMap = new HashMap<String, byte[]>();
			fileMap.put("pic", Base64.decode(img));
			String result = HttpUtil.upload("https://se.aft1v1.com/image_search/submit_image_and_get_result_co/", 10000, paramMap, fileMap);
			return result;
		}
		catch(Exception e) {
			throw new RuntimeException(e);
		}
	}
	public static String getAFanTiTrunk(String html) {
		Document doc = Jsoup.parse(html);
		doc.getElementsByClass("division_line").remove();
		if(!doc.getElementsByClass("post").isEmpty()) {
			doc.getElementsByClass("post").remove();
		}
		doc.getElementsByClass("knowledge_point").remove();
		doc.getElementsByClass("question_tag_wrapper").remove();
		doc.getElementsByClass("questionResult").attr("style", "width: 97%; padding-top: 10px; padding-bottom: 10px; font-size: medium; color: #000;");
		Elements elements = doc.getElementsByClass("Green_block");
		for(int i = 0; i < elements.size(); i++) {
			Element element = elements.get(i);
			String text = element.text().trim();
			if(!text.equals("题目") && element.parent().parentNode() != null) {
				element.parent().nextElementSibling().remove();
				element.parent().remove();
			}
			else {
				if(element.parent().parentNode() != null) {
					element.parent().remove();
				}
				else if(element.parentNode() != null){
					element.remove();
				}
			}
		}
		return doc.html().replace("$(document).ready(function () {", "$(document).ready(function () { return;");
	}

	public static String getAFanTiAnalys(String html) {
		Document doc = Jsoup.parse(html);
		Element mainElement = doc.getElementById("mainContent");
		mainElement.nextElementSibling().remove();
		mainElement.remove();
		doc.getElementsByClass("division_line").remove();
		if(!doc.getElementsByClass("post").isEmpty()) {
			doc.getElementsByClass("post").remove();
		}
		doc.getElementsByClass("knowledge_point").remove();
		doc.getElementsByClass("question_tag_wrapper").remove();
		doc.getElementsByClass("questionResult").attr("style", "width: 97%; padding-top: 10px; padding-bottom: 10px; font-size: medium; color: #000;");
		Elements elements = doc.getElementsByClass("Green_block");
		boolean find = false;
		for(int i = 0; i < elements.size(); i++) {
			Element element = elements.get(i);
			String text = element.text().trim();
			if(!text.equals("解析")) {
				if(element.parent() != null && element.parent().parentNode() != null) {
					if(element.parent().nextElementSibling() != null) {
						element.parent().nextElementSibling().remove();
					}
					element.parent().remove();
				}
			}
			else {
				find = true;
				if(element.parent().parentNode() != null) {
					element.parent().remove();
				}
				else if(element.parentNode() != null){
					element.remove();
				}
			}
		}
		if(find) {
			return doc.html();
		}
		return null;
	}

	public static String getAFanTiAnswer(String html) {
		Document doc = Jsoup.parse(html);
		Element mainElement = doc.getElementById("mainContent");
		mainElement.nextElementSibling().remove();
		mainElement.remove();
		doc.getElementsByClass("division_line").remove();
		if(!doc.getElementsByClass("post").isEmpty()) {
			doc.getElementsByClass("post").remove();
		}
		doc.getElementsByClass("knowledge_point").remove();
		doc.getElementsByClass("question_tag_wrapper").remove();
		doc.getElementsByClass("questionResult").attr("style", "width: 97%; padding-top: 10px; padding-bottom: 10px; font-size: medium; color: #000;");
		Elements elements = doc.getElementsByClass("Green_block");
		boolean find = false;
		for(int i = 0; i < elements.size(); i++) {
			Element element = elements.get(i);
			String text = element.text().trim();
			if(!(text.equals("答案") || text.equals("解答"))) {
				if(element.parent() != null && element.parent().parentNode() != null) {
					if(element.parent().nextElementSibling() != null) {
						element.parent().nextElementSibling().remove();
					}
					element.parent().remove();
				}
			}
			else if(text.equals("解答")){
				find = true;
			}
		}
		if(!find) {
			elements = doc.getElementsByClass("Green_block");
			for(int i = 0; i < elements.size(); i++) {
				Element element = elements.get(i);
				if(element.parent().parentNode() != null) {
					element.parent().remove();
				}
				else if(element.parentNode() != null){
					element.remove();
				}
			}
		}
		else {
			elements = doc.getElementsByClass("Green_block");
			for(int i = 0; i < elements.size(); i++) {
				Element element = elements.get(i);
				String text = element.text().trim();
				if(!text.equals("解答")) {
					if(element.parent() != null && element.parent().parentNode() != null) {
						if(element.parent().nextElementSibling() != null) {
							element.parent().nextElementSibling().remove();
						}
						element.parent().remove();
					}
				}
				else {
					if(element.parent().parentNode() != null) {
						element.parent().remove();
					}
					else if(element.parentNode() != null){
						element.remove();
					}
				}
			}
		}
		return doc.html().replace("$(document).ready(function () {", "$(document).ready(function () { return;");
	}
}
